//package com.fundodoo.sfe.admin.controller;
//
//import java.util.Arrays;
//import java.util.Collection;
//import java.util.List;
//import java.util.Locale;
//
//import org.apache.commons.collections.CollectionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.apache.shiro.authz.annotation.RequiresPermissions;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.MessageSource;
//import org.springframework.context.i18n.LocaleContextHolder;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import com.tpv.vc.common.annotation.Log;
//import com.tpv.vc.common.controller.BaseController;
//import com.tpv.vc.common.domain.PermissionInfo;
//import com.tpv.vc.common.domain.QueryRequest;
//import com.tpv.vc.common.domain.ResponseBo;
//import com.tpv.vc.common.domain.Tree;
//import com.tpv.vc.common.util.FileUtils;
//import com.tpv.vc.system.domain.Role;
//import com.tpv.vc.system.service.RoleMenuServie;
//import com.tpv.vc.system.service.RoleService;
//import com.tpv.vc.utils.StringUtil;
//
//@Controller
//public class RoleController extends BaseController {
//
//    private Logger log = LoggerFactory.getLogger(this.getClass());
//
//    @Autowired
//    private RoleService roleService;
//    
//    @Autowired
//    private RoleMenuServie roleMenuService;
//    
//    @Autowired
//    private MessageSource messageSource;
////    private Locale locale = LocaleContextHolder.getLocale();
//
//    @Log("访问角色信息页面")
//    @RequestMapping("role")
//    @RequiresPermissions("role:list")
//    public String index() {
//        return "system/role/role";
//    }
//    
//    @Log("获取用户组树菜单")
//    @RequestMapping("role/tree")
//    @ResponseBody
//    public ResponseBo getDeptTree(Role role) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	role.setParentId(null == permissionInfo.getRoleId() ? 0 : permissionInfo.getRoleId());
//        	role.setSelf("1");
//            Tree<Role> tree = this.roleService.getRoleTree(role);
//            return ResponseBo.ok(tree);
//        } catch (Exception e) {
//            log.error("获取用户组树失败", e);
//            return ResponseBo.error(messageSource.getMessage("dept.get.tree.fail", null, locale));
//        }
//    }
//
//    @Log("获取角色信息列表")
//    @RequestMapping("role/list")
//    @RequiresPermissions("role:list")
//    @ResponseBody
////    public Map<String, Object> roleList(QueryRequest request, Role role) {
////    	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
////    	role.setParentId(null == permissionInfo.getRoleId() ? 0 : permissionInfo.getRoleId());
////        return super.selectByPageNumSize(request, () -> this.roleService.findAllRole(role));
////    }
//	  public List<Role> roleList(QueryRequest request, Role role) {
//		  PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//		  role.setParentId(null == permissionInfo.getRoleId() ? 0 : permissionInfo.getRoleId());
//		  role.setSelf("0");
//		  return this.roleService.findAllRole(role);
//	  }
//
//    @Log("导出角色信息excel")
//    @RequestMapping("role/excel")
//    @ResponseBody
//    public ResponseBo roleExcel(Role role) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            List<Role> list = this.roleService.findAllRole(role);
//            return FileUtils.createExcelByPOIKit(messageSource.getMessage("role.table", null, locale), list, Role.class);
//        } catch (Exception e) {
//            log.error("导出角色信息Excel失败", e);
//            return ResponseBo.error(messageSource.getMessage("export.fail", null, locale));
//        }
//    }
//
//    @Log("导出角色信息csv")
//    @RequestMapping("role/csv")
//    @ResponseBody
//    public ResponseBo roleCsv(Role role) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            List<Role> list = this.roleService.findAllRole(role);
//            return FileUtils.createCsv(messageSource.getMessage("role.table", null, locale), list, Role.class);
//        } catch (Exception e) {
//            log.error("导出角色信息Csv失败", e);
//            return ResponseBo.error(messageSource.getMessage("export.fail", null, locale));
//        }
//    }
//
//    @Log("根据id获取角色信息")
//    @RequestMapping("role/getRole")
//    @ResponseBody
//    public ResponseBo getRole(Long roleId) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            Role role = this.roleService.findRoleWithMenus(roleId);
//            return ResponseBo.ok(role);
//        } catch (Exception e) {
//            log.error("获取角色信息失败", e);
//            return ResponseBo.error(messageSource.getMessage("role.get.fail", null, locale));
//        }
//    }
//
//    @RequestMapping("role/checkRoleName")
//    @ResponseBody
//    public boolean checkRoleName(String roleName, String oldRoleName) {
//        if (StringUtils.isNotBlank(oldRoleName) && roleName.equalsIgnoreCase(oldRoleName)) {
//            return true;
//        }
//        Role result = this.roleService.findByName(roleName);
//        return result == null;
//    }
//
//    @Log("新增角色")
//    @RequiresPermissions("role:add")
//    @RequestMapping("role/add")
//    @ResponseBody
//    public ResponseBo addRole(Role role, Long[] menuId) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	if(null == role.getParentId()) {
//        		role.setParentId(permissionInfo.getRoleId());
//        	}
//        	menuId = StringUtil.deleteArrayData(menuId, 0l);//删除根节点
//        	//modify by jm.hong at 20190122 子角色菜单不能超过父角色=====start=====
//        	List<Long> menuList=Arrays.asList(menuId);
//        	Collection<Long> disColl = getDisColl(role,menuList);
//        	if(CollectionUtils.isNotEmpty(disColl)) {
//        		return ResponseBo.error(messageSource.getMessage("role.children.menu.more.than.parent", null, locale));
//        	}
//        	//modify by jm.hong at 20190122 子角色菜单不能超过父角色=====end=====
//            this.roleService.addRole(role, menuId,permissionInfo.getRoleId());
//            return ResponseBo.ok(messageSource.getMessage("role.add.success", null, locale));
//        } catch (Exception e) {
//            log.error("新增角色失败", e);
//            return ResponseBo.error(messageSource.getMessage("role.add.fail", null, locale));
//        }
//    }
//
//    @Log("删除角色")
//    @RequiresPermissions("role:delete")
//    @RequestMapping("role/delete")
//    @ResponseBody
//    public ResponseBo deleteRoles(String ids) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//            this.roleService.deleteRoles(ids);
//            return ResponseBo.ok(messageSource.getMessage("role.delete.success", null, locale));
//        } catch (Exception e) {
//            log.error("删除角色失败", e);
//            return ResponseBo.error(messageSource.getMessage("role.delete.fail", null, locale));
//        }
//    }
//
//	@Log("修改角色")
//    @RequiresPermissions("role:update")
//    @RequestMapping("role/update")
//    @ResponseBody
//    public ResponseBo updateRole(Role role, Long[] menuId) {
//    	Locale locale = LocaleContextHolder.getLocale();
//        try {
//        	PermissionInfo permissionInfo = this.getCurrentUserPermissionInfo();
//        	if(null == role.getParentId()) {
//        		role.setParentId(permissionInfo.getRoleId());
//        	}
//        	//add by jm.hong 20190115 删除根节点(根节点只是用来展示)=====start=====         
//        	menuId = StringUtil.deleteArrayData(menuId, 0l);//删除根节点
//        	//add by jm.hong 20190115 删除根节点(根节点只是用来展示)=====end===== 
//        	//modify by jm.hong at 20190122 子角色菜单不能超过父角色=====start=====
//        	List<Long> menuList=Arrays.asList(menuId);//将数组转换为list集合
//        	Collection<Long> disColl = getDisColl(role,menuList);
//        	if(CollectionUtils.isNotEmpty(disColl)) {
//        		return ResponseBo.error(messageSource.getMessage("role.children.menu.more.than.parent", null, locale));
//        	}
//        	//modify by jm.hong at 20190122 子角色菜单不能超过父角色=====end=====
//        	
//            this.roleService.updateRole(role, menuId,permissionInfo.getRoleId());
//            return ResponseBo.ok(messageSource.getMessage("role.update.success", null, locale));
//        } catch (Exception e) {
//            log.error("修改角色失败", e);
//            return ResponseBo.error(messageSource.getMessage("role.update.fail", null, locale));
//        }
//    }
//	/**
//	 * 取父角色菜单和当前菜单的差集
//	 * @author jm.hong 20190122
//	 * @param role
//	 * @param menuList
//	 * @return Collection<Long>
//	 */
//    public Collection<Long> getDisColl (Role role,List<Long> menuList){
//    	Long parentId = role.getParentId();
//    	List<Long> parentMenuIds = roleMenuService.findMenuIdsByRoleId(parentId);
//    	Collection<Long> interColl = CollectionUtils.intersection(menuList, parentMenuIds);//取交集
//    	Collection<Long> disColl = CollectionUtils.disjunction(menuList, interColl);//取差集
//    	return disColl;
//    	
//    }
//    
//}
